@include("user.head")
	<!-- 公共头部 -->
	<div id="content_person">
	<div class="row">
		<div class="col-xs-3">
		@include('user.uc.navigation')
		</div>
		<div class="col-xs-9">
			<ul class="nav nav-tabs">
			  <li class="active"><a aria-expanded="true" href="#baseInfo" data-toggle="tab">设备基础信息</a></li>
			  <li class=""><a aria-expanded="false" href="#case" id="showCase" data-toggle="tab">设备案例</a></li>
			  <li class=""><a aria-expanded="false" href="#apply" id="showApply" data-toggle="tab">应用项目</a></li>
			</ul>
			<div id="myTabContent" class="tab-content">
			  	<div class="tab-pane fade active in" id="baseInfo">
		    		<div class="panel panel-default">
		    			<!-- <div class="panel-heading">
		    				<h3 class="panel-title">添加/编辑设备</h3>
		    			</div> -->
		    			<div class="panel-body">
		    				<div class="form-horizontal">
		    				  <fieldset>
		    				    <div class="form-group">
		    				      <label class="col-xs-3 control-label no_padding_right">设备名称:</label>
		    				      <div class="col-xs-6">
		    				        <input class="form-control input_size" name="name"  placeholder="" type="text">
		    				      </div>
		    				    </div>

		    				    <div class="form-group">
		    				      <label class="col-xs-3 control-label no_padding_right">设备型号:</label>
		    				      <div class="col-xs-6">
		    				        <input class="form-control input_size" name="model"  placeholder="" type="text">
		    				      </div>
		    				    </div>

		    				    <div class="form-group">
		    				      <label class="col-xs-3 control-label no_padding_right">制造商:</label>
		    				      <div class="col-xs-6">
		    				        <input class="form-control input_size" name="creator"  placeholder="" type="text">
		    				      </div>
		    				    </div>

		    				    <div class="form-group">
		    				      <label class="col-xs-3 control-label no_padding_right">认证信息:</label>
		    				      <div class="col-xs-6">
		    				        <input class="form-control input_size" name="auth_info"  placeholder="" type="text">
		    				      </div>
		    				    </div>


		    				    <div class="form-group">
		    				      <label class="col-xs-3 control-label no_padding_right">产品标准:</label>
		    				      <div class="col-xs-6">
		    				        <input class="form-control input_size" name="standard"  placeholder="" type="text">
		    				      </div>
		    				    </div>

		    				    <div class="form-group">
		    				      <label class="col-xs-3 control-label no_padding_right">设备最低价格:</label>
		    				      <div class="col-xs-6">
		    				        <input class="form-control input_size" name="minimize_price"  placeholder="" type="text">
		    				      </div>
		    				    </div>

		    				    <div class="form-group">
		    				      <label class="col-xs-3 control-label no_padding_right">设备最高价格:</label>
		    				      <div class="col-xs-6">
		    				        <input class="form-control input_size" name="maximize_price"  placeholder="" type="text">
		    				      </div>
		    				    </div>

		    		    	    <div class="form-group">
		    		          		<label for="select" class="col-xs-3 control-label no_padding_right">应用技术:</label>
		    		    	      	<div class="col-xs-6">
		    		    	        <select class="form-control input_size" name="tech_id">
		    		    	          <option value="">请选择</option>
		    		    	          @foreach($techs as $value)
		    		    	          <option value="<%$value['id']%>"><%$value['cate_name']%></option>
		    		    	          @endforeach
		    		    	        </select>
		    		    	      </div>
		    		        	</div>

		    		        	<div class="form-group" >
		    		          		<label for="select" class="col-xs-3 control-label no_padding_right">应用领域:</label>
		    		    	      	<div class="col-xs-6">
		    		    	        <select class="form-control input_size" name="domain_id" id="domain">
		    		    	          <option value="0">请选择</option>
		    		    	          @foreach($domains as $value)
		    		    	          <option value="<%$value['id']%>"><%$value['cate_name']%></option>
		    		    	          @endforeach
		    		    	        </select>
		    		    	     	</div>
		    		        	</div>
		            	    	<div class="form-group" >
		            	      		<label for="select" class="col-xs-3 control-label no_padding_right"></label>
		            		      	<div class="col-xs-6" id="domains">
		            	     		</div>
		            	    	</div>
		            		    <div class="form-group mainBody">
		            		      <label class="col-xs-3 control-label no_padding_right">应用主体:</label>
		            		      <div class="col-xs-6">
		            		        <select class="form-control input_size main_body">
		    		    	          	<option value="">请选择</option>
		    		    	          	@foreach ($mBody as $value)
		    		    	          	<option value="<%$value['id']%>"><%$value['name']%></option>
		    		    	          	@endforeach
		            		        </select>
		            		      </div>
		            		      <div class="col-xs-3 no_padding_both">
		            		      	<button class="btn btn-primary" id="addMain">添加</button>
		            		      </div>
		            		    </div>

		    				    <div class="form-group">
		    				      <label class="col-xs-3 control-label no_padding_right">设备图片:</label>
		    				      <div class="col-xs-6">
		    				        <a href="javascript:;" class="file btn btn-primary">上传
		    				            <input type="file" name="imgFile" id="pic">
		    				            <input type="hidden" class="hiddenName" name="image">
		    				        </a><span class="fileName"></span>
		    				      </div>
		    				    </div>

		    				    <div class="form-group">
		    				      <label for="inputEmail" class="col-xs-3 control-label no_padding_right">节能效率:</label>
		    				      <div class="col-xs-6">
		    				        <input class="form-control input_size" name="efficiency" autocomplete="of" id="inputEmail" placeholder="" type="text">
		    				      </div>
		    				    </div>

		    				    <div class="form-group">
		    				      <label for="textArea" class="col-xs-3 control-label no_padding_right">设备功能:</label>
		    				      <div class="col-xs-6">
		    				        <textarea class="form-control text_size" rows="3" name="function_name" id="textArea"></textarea>
		    				        <span class="help-block"></span>
		    				      </div>
		    				    </div>

		    				    <div class="form-group">
		    				      <label for="textArea" class="col-xs-3 control-label no_padding_right">设备特点:</label>
		    				      <div class="col-xs-6">
		    				        <textarea class="form-control text_size" rows="3" name="features_name" id="textArea"></textarea>
		    				        <span class="help-block"></span>
		    				      </div>
		    				    </div>

		    				    <div class="form-group">
		    				      <label for="textArea" class="col-xs-3 control-label no_padding_right">设备描述:</label>
		    				      <div class="col-xs-6">
		    				        <textarea class="form-control text_size" rows="3" name="description" id="textArea"></textarea>
		    				        <span class="help-block"></span>
		    				      </div>
		    				    </div>

		    			    	<div class="form-group">
		    			    		<label for="textArea" class="col-xs-3 control-label no_padding_right">设备原理:</label>
		    			          	<div class="col-xs-9">
		    			            	<div id="container" style="height:500px;"></div>
		    			          	</div>
		    			        </div>


		    				    <div class="form-group">
		    				      <div class="col-xs-12 text-center">
		    				      	<input type="hidden" name="did">
		    				        <button type="button" id="sub" class="btn btn-primary ">提交</button>
		    				      </div>
		    				    </div>
		    				  </fieldset>
		    				</div>
		    			</div>
		    		</div>
			  	</div>
			  	<div class="tab-pane fade" id="case">
					<div class="panel panel-default">
						<!-- <div class="panel-heading">
							<h3 class="panel-title">添加/编辑设备</h3>
						</div> -->
						<div class="panel-body">
							<div class="form-horizontal">
							  <fieldset>
							    <div class="form-group">
							      <label class="col-xs-3 control-label no_padding_right">案例名称:</label>
							      <div class="col-xs-6">
							        <input class="form-control input_size" name="name"  placeholder="" type="text">
							      </div>
							    </div>

							    <div class="form-group">
							      <label for="textArea" class="col-xs-3 control-label no_padding_right">案例描述:</label>
							      <div class="col-xs-6">
							        <textarea class="form-control text_size" rows="3" name="content" id="textArea"></textarea>
							        <span class="help-block"></span>
							      </div>
							    </div>

							    <div class="form-group">
							      <label class="col-xs-3 control-label no_padding_right">开始时间:</label>
							      <div class="col-xs-6">
							        <input class="form-control input_size date-time-picker" readonly name="start_time"  placeholder="" type="text">
							      </div>
							    </div>

							    <div class="form-group">
							      <label class="col-xs-3 control-label no_padding_right">结束时间:</label>
							      <div class="col-xs-6">
							        <input class="form-control input_size date-time-picker" readonly name="end_time"  placeholder="" type="text">
							      </div>
							    </div>

							    <div class="form-group">
							      <div class="col-xs-12 text-center">
							      	<input type="hidden" name="cid">
							        <button type="button" id="addCases" class="btn btn-primary ">添加</button>
							      </div>
							    </div>

							      	<table class="table table-striped table-hover text-center table_marginTop">
							    	  <thead>
							    	      <td><b>案例名称</b></td>
							    	      <td><b>案例内容</b></td>
							    	      <td><b>开始时间</b></td>
							    	      <td><b>结束时间</b></td>
							          	  <td><b>操作</b></td>
							    	  </thead>
							    	  <tbody id="casesLists">
							    	  </tbody>
							    	</table>
							  </fieldset>
							</div>
						</div>
					</div>
			  	</div>
			  	<div class="tab-pane fade" id="apply">
    		    	<div class="panel panel-default">
    		    		<div class="panel-body">
    		    			<div class="form-horizontal">
    		    			  <fieldset>
		            		    <div class="form-group applyProject">
		            		      <label class="col-xs-3 control-label no_padding_right">应用案例:</label>
		            		      <div class="col-xs-6">
		            		        <select class="form-control input_size" id="apply_project">
		    		    	          	<option value="">请选择</option>
		    		    	          	@foreach($allProject as $value)
		    		    	          	<option value="<%$value['id']%>"><%$value['name']%></option>
		    		    	          	@endforeach
		            		        </select>
		            		      </div>
		            		      <div class="col-xs-3 no_padding_both">
    	            		      	<button class="btn btn-primary" id="addApply">添加</button>
		            		      </div>
		            		    </div>
		            		      	<table class="table table-striped table-hover text-center table_marginTop">
		            		    	  <thead>
		            		    	      <td><b>应用项目名称</b></td>
		            		    	      <td><b>开始时间</b></td>
		            		    	      <td><b>结束时间</b></td>
		            		          	  <td><b>操作</b></td>
		            		    	  </thead>
		            		    	  <tbody id="applyLists">
		            		    	  </tbody>
		            		    	</table>
    		    			  </fieldset>
    		    			</div>
    		    		</div>
    		    	</div>
			  	</div>
			</div>

		</div>
	</div>
	</div>
	<!-- 公共底部 -->
	@include("user.footer")
	<!-- 公共底部 -->
	@include("user.common")
</body>
<script type="text/html" id="mainbodyTemp">
<option value="">请选择</option>
{%each list as value}
<option value="{%value.id}">{%value.name}</option>
{%/each}
</script>

<script type="text/html" id="mainSelectTemp">
<div class="form-group mainBody">
  <label class="col-xs-3 control-label no_padding_right"></label>
  <div class="col-xs-6">
    <select class="form-control input_size main_body">
    </select>
  </div>
  <div class="col-xs-3 no_padding_both">
  	<button class="btn btn-warning deleteMain">删除</button>
  </div>
</div>
</script>

<script type="text/html" id="domain_type">
	<select class="form-control input_size" name="domain_ids">
		{%each list as value index}
		<option value="{%value.id}">{%value.cate_name}</option>
		{%/each}
	</select>
</script>

<script type="text/html" id="casesList">
{%each list as value index}
<tr>
  <td>{%value.name}</td>
  <td>{%truncateText(value.content, 20) }</td>
  <td>{%value.start_time}</td>
  <td>{%value.end_time}</td>
  <td>
  	<button class="btn btn-primary editCase" cid="{%value.id}" title="编辑">
  		<i class="fa fa-pencil-square-o"></i>
  	</button>
  	<button class="btn btn-primary deleteCase" cid="{%value.id}" title="删除">
  		<i class="fa fa-pencil-square-o"></i>
  	</button>
  </td>
</tr>
{%/each}
</script>


<script type="text/html" id="applyList">
{%each list as value index}
<tr>
  <td>{%value.project.name}</td>
  <td>{%value.project.start_time}</td>
  <td>{%value.project.end_time}</td>
  <td>
  	<button class="btn btn-primary deleteApply" aid="{%value.id}" title="删除">
  		<i class="fa fa-pencil-square-o"></i>
  	</button>
  </td>
</tr>
{%/each}
</script>


<script type="text/javascript">

var ue = UE.getEditor('container');

if("<%Input::has('did')%>"){
	var did = "<%Input::get('did')%>";
	$.get("edit_device/get_device", {did :did},function(data){
		if(data.success){
			console.log(data);
			isasync = false;
			$("#baseInfo input[name='name']").val(data.name);
			$("#baseInfo input[name='model']").val(data.model);
			$("#baseInfo input[name='creator']").val(data.creator);
			$("#baseInfo input[name='auth_info']").val(data.auth_info);
			$("#baseInfo input[name='standard']").val(data.standard);
			$("#baseInfo input[name='minimize_price']").val(data.minimize_price);
			$("#baseInfo input[name='maximize_price']").val(data.maximize_price);
			$("#baseInfo input[name='efficiency']").val(data.efficiency);
			$("#baseInfo input[name='image']").val(data.image);
			$("#baseInfo select[name='tech_id']").val(data.device_tech.cate_id);
			$("#baseInfo select[name='domain_id']").val(data.device_domain.domain.parent_id);
			$("#baseInfo select[name='domain_id']").change();
			$("#baseInfo select[name='domain_ids']").val(data.device_domain.domain.id);
			$("#baseInfo input[name='did']").val(data.id);
			$("#baseInfo textarea[name='description']").val(data.description);
			$("#baseInfo textarea[name='function_name']").val(data.device_functions[0].content);
			$("#baseInfo textarea[name='features_name']").val(data.device_features[0].content);

			$.each(data.mainbody, function(k, v){
				$(".main_body").eq(k).val(v.mb_id);
				$("#addMain").click();
			})
			$(".deleteMain").last().click();
			isasync = true;
			ue.ready(function(){
			    ue.setContent(decodeURIComponent(data.theory));
			});
		}
	})
}
$("#showApply").click(function(){
	$.get("edit_device/get_device_apply", {did : did}, function (data){
		if(data.success && data.data.length){
			var html = template("applyList", {list : data.data});
			$("#applyLists").html(html);
			$(".deleteApply").unbind("click");
			$(".deleteApply").bind("click", function(){
				deleteApply($(this));
			})
		}
	})
})

function deleteApply(_this){
	var aid = _this.attr("aid");
	$.post("edit_device/delete_apply", {aid : aid, did : did}, function (data){
		if(data.success){
			var html = template("applyList", {list : data.data});
			$("#applyLists").html(html);
			$(".deleteApply").unbind("click");
			$(".deleteApply").bind("click", function(){
				deleteApply($(this));
			});
		}
	})
}

// 应用案例
$("#addApply").click(function(){
	var pid = $("#apply_project").val();
	if(pid < 1){
		warning("", "请选择应用项目");
		return;
	}
	$.post("edit_device/save_apply", {pid :pid, did : did}, function (data){
		var html = template("applyList", {list : data.data});
		$("#applyLists").html(html);
		$(".deleteApply").unbind("click");
		$(".deleteApply").bind("click", function(){
			deleteApply($(this));
		})
	})
})


var curl = "edit_device/add_case";
$("#showCase").click(function(){
	$.get("edit_device/get_device_cases", {did : did}, function (data){
		if(data.success && data.data.length){
			var html = template("casesList", {list : data.data});
			$("#casesLists").html(html);
			$(".editCase").bind("click", function(){
				editCase($(this));
			})
			$(".deleteCase").bind("click", function(){
				deleteCase($(this));
			})


		}
	})
})
$("#addCases").click(function(){
	var caseName = $("#case input[name='name']").val().trim();
	var caseContent = $("#case textarea[name='content']").val().trim();
	var caseStart = $("#case input[name='start_time']").val().trim();
	var caseEnd = $("#case input[name='end_time']").val().trim();
	var cid = $("#case input[name='cid']").val().trim();
	$.post(curl, {
		name : caseName,
		content : caseContent,
		start_time : caseStart,
		end_time : caseEnd,
		cid : cid,
		device_id : did
	}, function (data){
		if(data.success){
			$("#case input[name='name']").val('');
			$("#case textarea[name='content']").val('');
			$("#case input[name='start_time']").val('');
			$("#case input[name='end_time']").val('');
			$("#case input[name='cid']").val('');
			curl = "edit_device/add_case";
			var html = template("casesList", {list : data.data});
			$("#casesLists").html(html);
			$(".editCase").unbind("click");
			$(".editCase").bind("click", function(){
				editCase($(this));
			})
			$(".deleteCase").unbind("click");
			$(".deleteCase").bind("click", function(){
				deleteCase($(this));
			})
		}
	})
})

function editCase(_this){
	var cid = _this.attr("cid");
	$.get("edit_device/get_case_detail", {cid : cid}, function (data){
		$("#case input[name='name']").val(data.data.name);
		$("#case textarea[name='content']").val(data.data.content);
		$("#case input[name='start_time']").val(data.data.start_time);
		$("#case input[name='end_time']").val(data.data.end_time);
		$("#case input[name='cid']").val(cid);
		curl = "edit_device/update_case";
	})
}

function deleteCase(_this){
	var cid = _this.attr("cid");
	$.post("edit_device/delete_cases", {cid : cid, did : did}, function (data){
		if(data.success){
			var html = template("casesList", {list : data.data});
			$("#casesLists").html(html);
			$(".deleteCase").unbind("click");
			$(".deleteCase").bind("click", function(){
				deleteCase($(this));
			})
			$(".editCase").unbind("click");
			$(".editCase").bind("click", function(){
				editCase($(this));
			})
		}
	})
}



$("#addMain").bind("click", function(){
	var html = template("mainSelectTemp", {});
	$(html).insertAfter($(".mainBody").last());
	loadMain();
	$(".deleteMain").unbind("click");
	$(".deleteMain").bind("click", function(){
		$(this).parents(".mainBody").remove();
	})
})


function loadMain(){
	$.ajax({
		url : "<%url('uc/get_mainbody')%>",
		type : "get",
		async : isasync,
		success : function(data){
			if(data.success && data.data.length)
			{
				var str = template("mainbodyTemp", {list : data.data});
				$(".main_body").last().html(str);
			}
		}
	})
}


$('#pic').AjaxFileUpload({
	path : "<% url('uc/user/upload') %>",
	onComplete : function(filename){
			$(this).siblings('input[class="hiddenName"]').val(filename.filename);
			$(this).parent(".file").siblings(".fileName").html(filename.filename);
		}
	});
var isasync = true;
var url = "edit_device/add";
$("#domain").change(function(){
	var parent_id = $(this).val();
	$.ajax({
		url : "<%url('uc/get_domain_type')%>",
		type : "get",
		data : {parent_id : parent_id},
		async: isasync,
		dataType : "json",
		success:function(data){
			if(data.success){
				var html = template("domain_type", {list: data.data});
				$("#domains").html(html);
			}
		},
		error:function(data){
			var html = '';
			$("#domains").html(html);
		}
	})

})





$("#sub").click(function(){
	var domain = $("#baseInfo select[name='domain_id']").val();
	var domains = $("#baseInfo select[name='domain_ids']").val();
	if(domains)
	{
		var domain_id = domains;
	}
	else
	{
		var domain_id = domain;
	}
	var tech_id = $("#baseInfo select[name='tech_id']").val();
	var name = $("#baseInfo input[name='name']").val().trim();
	var model = $("#baseInfo input[name='model']").val().trim();
	var creator = $("#baseInfo input[name='creator']").val().trim();
	var auth_info = $("#baseInfo input[name='auth_info']").val().trim();
	var standard = $("#baseInfo input[name='standard']").val().trim();
	var minimize_price = $("#baseInfo input[name='minimize_price']").val().trim();
	var maximize_price = $("#baseInfo input[name='maximize_price']").val().trim();
	var efficiency = $("#baseInfo input[name='efficiency']").val().trim();
	var image = $("#baseInfo input[name='image']").val().trim();
	var did = $("#baseInfo input[name='did']").val().trim();
	var description = $("#baseInfo textarea[name='description']").val().trim();
	var theory = encodeURIComponent(ue.getContent());
	var function_name = $("#baseInfo textarea[name='function_name']").val().trim();
	var features_name = $("#baseInfo textarea[name='features_name']").val().trim();

	var main_body = [];
	$(".main_body").each(function(k, v){
		if($(v).val() > 0){
			main_body.push($(v).val());
		}
	})

	$.post("edit_device/update", {
		name : name,
		model : model,
		creator : creator,
		auth_info : auth_info,
		standard : standard,
		minimize_price : minimize_price,
		maximize_price : maximize_price,
		efficiency : efficiency,
		image : image,
		description : description,
		theory : theory,
		domain_id : domain_id,
		tech_id : tech_id,
		did : did,
		main_body : main_body,
		features_name : features_name,
		function_name : function_name
	}, function (data){
		if(data.success){
			success('', '提交成功');
			setTimeout(function(){
				window.location.href="device";
			},2000)
		}
	})
})



</script>
</html>